package cn.sh.org.controller;

import cn.sh.basic.annotation.PreAuthorize;
import cn.sh.basic.page.PageList;
import cn.sh.basic.util.AjaxResult;
import cn.sh.org.domain.Department;
import cn.sh.org.domain.Employee;
import cn.sh.org.query.DepartmentQuery;
import cn.sh.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /*查询所有*/
    @GetMapping
    public List<Department> selectAll(){
        return departmentService.selectAll();
    }

    /*分页查询
    * @RequestBody作用 把参数转为json字符串 放到请求体中  然后取值
    * */
    @PostMapping
    public PageList<Department> selectPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryData(departmentQuery);
    }

    /*查询一条
    * @PathVariable("id") 作用：从url路径中取到参数值
    * */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department selectOne(@ApiParam(name="ID属性",required = true) @PathVariable("id") Long id){
        return departmentService.selectOne(id);
    }

    /*删除一个*/
    @DeleteMapping("/{id}")
    @PreAuthorize(name="删除按钮",value = "department:deleteById")
    public AjaxResult deleteDepartment(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMesssage("操作失败");
        }
    }

    /*新增和修改*/
    @PutMapping
    public AjaxResult addOneUpdateDepartment(@RequestBody Department department){
        System.out.println(department);
        try {
            if(department.getId() == null){
                //新增
                departmentService.add(department);
            }else {
                //修改
                Employee manager = department.getManager();
                System.out.println(manager);
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMesssage("操作失败");
        }
    }

    /*批量删除*/
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.batchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMesssage("操作失败");
        }
    }

    /*获取部门树状结构*/
    @GetMapping("/getTree")
    public List<Department> getTree(){
        List<Department> tree = departmentService.getTree();
        return tree;
    }
}
